package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.system.service.SysOssService;
import com.ruoyi.wms.domain.bo.employee.EmployeesBo;
import com.ruoyi.wms.domain.entity.employee.Employees;
import com.ruoyi.wms.domain.vo.EmployeesVo;
import com.ruoyi.wms.domain.vo.NameAndIdList;
import com.ruoyi.wms.mapper.EmployeesMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 员工信息Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-23
 */
@RequiredArgsConstructor
@Service
public class EmployeesService {

    private final EmployeesMapper employeesMapper;
    private final SysOssService sysOssService;

    /**
     * 查询员工信息
     */
    public EmployeesVo queryById(Long id) {
        return employeesMapper.selectVoById(id);
    }

    /**
     * 查询员工信息列表
     */
    public TableDataInfo<EmployeesVo> queryPageList(EmployeesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Employees> lqw = buildQueryWrapper(bo);
        Page<EmployeesVo> result = employeesMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(
            vo -> {
                vo.setPhotoPath(sysOssService.selectUrlByIds(vo.getPhotoPath()));
            }
        );
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工信息列表
     */
    public List<EmployeesVo> queryList(EmployeesBo bo) {
        LambdaQueryWrapper<Employees> lqw = buildQueryWrapper(bo);
        return employeesMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Employees> buildQueryWrapper(EmployeesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Employees> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreId() != null, Employees::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNumber()), Employees::getEmployeeNumber, bo.getEmployeeNumber());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Employees::getName, bo.getName());
        lqw.eq(bo.getPositionId() != null, Employees::getPositionId, bo.getPositionId());
        return lqw;
    }

    /**
     * 新增员工信息
     */
    public void insertByBo(EmployeesBo bo) {
        Employees add = MapstructUtils.convert(bo, Employees.class);
        employeesMapper.insert(add);
    }

    /**
     * 修改员工信息
     */
    public void updateByBo(EmployeesBo bo) {
        Employees update = MapstructUtils.convert(bo, Employees.class);
        employeesMapper.updateById(update);
    }

    /**
     * 批量删除员工信息
     */
    public void deleteByIds(Collection<Long> ids) {
        employeesMapper.deleteBatchIds(ids);
    }

    /**
     * 返回所有员工的id和名称
     *
     * @return
     */
    public List<NameAndIdList> list() {
        List<Employees> employees = employeesMapper.selectList();
        List<NameAndIdList> employeeList = new ArrayList<>();
        for (Employees employee : employees) {
            NameAndIdList list = new NameAndIdList();
            list.setId(employee.getId());
            list.setName(employee.getName());
            employeeList.add(list);
        }
        return employeeList;
    }
}
